package Group9.TodoManager.View;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Group9.TodoManager.Settings;
import Group9.TodoManager.Actions.RemoveListItemAction;
import Group9.TodoManager.Actions.SelectListAction;
import Group9.TodoManager.Controller.DateController;
import Group9.TodoManager.Controller.ITaskController;
import Group9.TodoManager.Data.Date;
import Group9.TodoManager.Data.SimpleListModel;
import Group9.TodoManager.Data.Task;
import Group9.TodoManager.Data.TaskCellRenderer;

/**
 * ListUI class is the pop up List of the application
 * It shows today's tasks in the list.
 * @author volkan cambazoglu
 *
 */
public class ListUI extends JFrame{

    private JPanel panel;
    private JList list;
    private JScrollPane scrollList;
    private SimpleListModel tasks;
    private ITaskController<Task> controller;
    private RemoveListItemAction removeAction;

    /**
     * Default Constructor. Creates an instance of the class.
     * Initializes the components of the UI.
     */
    public ListUI() {
        super();

        controller = Settings.getTaskController();
        JPanel jpanel = new JPanel();
        jpanel.setLayout(new BorderLayout());

        panel = new JPanel();
        panel.setLayout(new GridLayout(0, 3));
        
        removeAction = new RemoveListItemAction(
                Settings.getCurrentResourceBundle().getString("DeleteAction"),
                controller.getIcon("/Group9/TodoManager/Resource/Delete.png"),
                Settings.getCurrentResourceBundle().getString("DeleteAction"),
                this);
        
        removeAction.setEnabled(false);
        
        list = new JList();
        initializeList();
        scrollList = new JScrollPane(list);

        JLabel label1 = new JLabel(
        		Settings.getCurrentResourceBundle().getString("TaskUI.Title"));
        JLabel label2 = new JLabel(
        		Settings.getCurrentResourceBundle().getString("TaskUI.BeginDate"));
        JLabel label3 = new JLabel(
        		Settings.getCurrentResourceBundle().getString("TaskUI.EndDate"));

        panel.add(label1);
        panel.add(label2);
        panel.add(label3);

        jpanel.add(panel, BorderLayout.PAGE_START);
        jpanel.add(scrollList, BorderLayout.CENTER);

        add(jpanel);
        setBounds(100, 100, 400, 400);
        setTitle(Settings.getCurrentResourceBundle().getString("Title.Today"));
    }

    /**
     * Initializes the list by retrieving tasks from the table model
     */
    public void initializeList() {

        List<Object> listOfTasks = new ArrayList<Object>();
        int numberOfElements = Settings.getTableModel().getRowCount();
        for (int i = 0; i < numberOfElements; i++) {
            Task task = Settings.getTableModel().getRowObject(i);
            if (DateController.isSameDay(task.getBeginDate(), new Date()) ||
	            DateController.isSameDay(task.getEndDate(), new Date()) ||
	           (DateController.compareDates(task.getBeginDate(), new Date()) != 1 &&
	            DateController.compareDates(task.getEndDate(), new Date()) != -1)) {
            	
                listOfTasks.add(task);
            }
        }
        tasks = new SimpleListModel(listOfTasks);
        list.setModel(tasks);
        list.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {

                    public void valueChanged(ListSelectionEvent e) {
                        SelectListAction select = new SelectListAction(tasks);
                        select.executeAction(
                                Integer.valueOf(
                                list.getSelectionModel().getMinSelectionIndex()));
                        removeAction.setEnabled(true);
                    }
                });

        list.setComponentPopupMenu(getListRowContextMenu());        
        list.setCellRenderer(new TaskCellRenderer());
        list.getSelectionModel().setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);
    }

    /**
     * This returns the context menu instance for the JList
     * @return
     */
    private JPopupMenu getListRowContextMenu() {
        JPopupMenu menu = new JPopupMenu();

        menu.add(new JMenuItem(removeAction));

        return menu;
    }

    /**
     * Getter method for the list
     * @return list JList
     */
    public JList getList() {
        return list;
    }

    /**
     * Getter method for the list model
     * @return listModel SimpleListModel
     */
    public SimpleListModel getListModel() {
        return tasks;
    }
    
    /**
     * Remove the specified task from the list
     * @param obj task
     */
    public void removeFromList(Object obj){
    	tasks.removeElement(obj);
    	tasks = tasks.copyModel();
        list.setModel(tasks);
        list.validate();
        Settings.getMainGUI().setUpcomingList();
    }
    
    /**
     * Add the specified task to the list
     * @param obj task
     */
    public void addToList(Object obj){
    	tasks.addElement(obj);
    	tasks = tasks.copyModel();
        list.setModel(tasks);
        list.validate();
        Settings.getMainGUI().setUpcomingList();
    }
    
    /**
     * Update the task that is located at the index with the new one
     * @param obj new task
     * @param index index of the previous task
     */
    public void updateList(Object obj, int index){
    	tasks.setElement(index, obj);
    	tasks = tasks.copyModel();
        list.setModel(tasks);
        list.validate();
        Settings.getMainGUI().setUpcomingList();
    }
}
